package com.binaryTree;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;

public class NaryTreeLevelOrderTraversal0209_429 {
    class Node {
        public int val;
        public List<Node> children;

        public Node() {}

        public Node(int _val) {
            val = _val;
        }

        public Node(int _val, List<Node> _children) {
            val = _val;
            children = _children;
        }
    }

    public List<List<Integer>> levelOrder(Node root) {
        if(root == null) return new ArrayList<>();
        Queue<Node> queue = new LinkedList<>();
        List<List<Integer>> res = new ArrayList<>();

        queue.offer(root);

        while (!queue.isEmpty()){
            int len = queue.size();
            List<Integer> item = new ArrayList<>();
            while (len > 0){
                Node node = queue.poll();
                for (Node child : node.children) {
                    if(child != null) queue.offer(child);
                }
                item.add(node.val);
                len--;
            }
            res.add(item);
        }
        return res;
    }
}



